package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportRhlqYcjldnfxjlbBaDetail;
import com.ruoyi.report_lq.domain.vo.ReportRhlqYcjldnfxjlbBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqZfclwhljlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqZfclwhljlbBaDetailVo;
import com.ruoyi.report_lq.domain.ReportRhlqZfclwhljlbBaDetail;
import com.ruoyi.report_lq.mapper.ReportRhlqZfclwhljlbBaDetailMapper;
import com.ruoyi.report_lq.service.IReportRhlqZfclwhljlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青蒸发残留物含量记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqZfclwhljlbBaDetailServiceImpl implements IReportRhlqZfclwhljlbBaDetailService {

    private final ReportRhlqZfclwhljlbBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青蒸发残留物含量记录
     */
    @Override
    public ReportRhlqZfclwhljlbBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }


    /**
     *
     * @param infoId
     * @return 黏附性集合
     * @method 根据info-id 查询蒸发残留物的list集合数据
     */
    public List<ReportRhlqZfclwhljlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqZfclwhljlbBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询乳化沥青蒸发残留物含量记录列表
     */
    @Override
    public TableDataInfo<ReportRhlqZfclwhljlbBaDetailVo> queryPageList(ReportRhlqZfclwhljlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqZfclwhljlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青蒸发残留物含量记录列表
     */
    @Override
    public List<ReportRhlqZfclwhljlbBaDetailVo> queryList(ReportRhlqZfclwhljlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqZfclwhljlbBaDetail> buildQueryWrapper(ReportRhlqZfclwhljlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportRhlqZfclwhljlbBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportRhlqZfclwhljlbBaDetail::getYangpbh, bo.getYangpbh());
        lqw.eq(bo.getShiyrqblbhjzlA() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbhjzlA, bo.getShiyrqblbhjzlA());
        lqw.eq(bo.getShiyrqblbjryhjzlA() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbjryhjzlA, bo.getShiyrqblbjryhjzlA());
        lqw.eq(bo.getShiyrqblbjclwhjzlA() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbjclwhjzlA, bo.getShiyrqblbjclwhjzlA());
        lqw.eq(bo.getZhengfclwhlA() != null, ReportRhlqZfclwhljlbBaDetail::getZhengfclwhlA, bo.getZhengfclwhlA());
        lqw.eq(bo.getZhengfclwhlpjz() != null, ReportRhlqZfclwhljlbBaDetail::getZhengfclwhlpjz, bo.getZhengfclwhlpjz());
        lqw.eq(bo.getShiyrqblbhjzlB() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbhjzlB, bo.getShiyrqblbhjzlB());
        lqw.eq(bo.getShiyrqblbjryhjzlB() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbjryhjzlB, bo.getShiyrqblbjryhjzlB());
        lqw.eq(bo.getShiyrqblbjclwhjzlB() != null, ReportRhlqZfclwhljlbBaDetail::getShiyrqblbjclwhjzlB, bo.getShiyrqblbjclwhjzlB());
        lqw.eq(bo.getZhengfclwhlB() != null, ReportRhlqZfclwhljlbBaDetail::getZhengfclwhlB, bo.getZhengfclwhlB());
        return lqw;
    }

    /**
     * 新增乳化沥青蒸发残留物含量记录
     */
    @Override
    public Boolean insertByBo(ReportRhlqZfclwhljlbBaDetailBo bo) {
        ReportRhlqZfclwhljlbBaDetail add = BeanUtil.toBean(bo, ReportRhlqZfclwhljlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青蒸发残留物含量记录
     */
    @Override
    public Boolean updateByBo(ReportRhlqZfclwhljlbBaDetailBo bo) {
        ReportRhlqZfclwhljlbBaDetail update = BeanUtil.toBean(bo, ReportRhlqZfclwhljlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 乳化沥青蒸发残留物含量 记录表  -- 子表操作
     */
    public Boolean insertOrUpdateByClwBo(List<ReportRhlqZfclwhljlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportRhlqZfclwhljlbBaDetail detail = new ReportRhlqZfclwhljlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);

            detail.setYangpbh(detailVo.getYangpbh());
            detail.setShiyrqblbhjzlA(detailVo.getShiyrqblbhjzlA());
            detail.setShiyrqblbjryhjzlA(detailVo.getShiyrqblbjryhjzlA());
            detail.setShiyrqblbjclwhjzlA(detailVo.getShiyrqblbjclwhjzlA());
            detail.setZhengfclwhlA(detailVo.getZhengfclwhlA());

            detail.setZhengfclwhlpjz(detailVo.getZhengfclwhlpjz());

            detail.setZhengfclwhlB(detailVo.getZhengfclwhlB());
            detail.setShiyrqblbhjzlB(detailVo.getShiyrqblbhjzlB());
            detail.setShiyrqblbjryhjzlB(detailVo.getShiyrqblbjryhjzlB());
            detail.setShiyrqblbjclwhjzlB(detailVo.getShiyrqblbjclwhjzlB());

            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqZfclwhljlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青蒸发残留物含量记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
